#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@author: KevinQi
@file: __init__.py
@time: 2023/12/25 15:16
@desc: 祝参与本系统的guys~圣诞节快乐!
"""


from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, text
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    age = Column(Integer)

    @classmethod
    def update(cls, filter, value):
        cls.session.query(cls).filter(text(filter)).update(value, synchronize_session=False)
        cls.session.commit()

engine = create_engine('sqlite:///example.db')
Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)
session = Session()

# 假设我们有一个User对象，我们想要更新它的name和age属性
user = User.query.filter(User.name == 'old_name').first()
User.update("name = 'old_name'", {'name': 'new_name', 'age': 25})

